/**
 * @fileOverview
 * @author rekey
 * Created by rekey on 23/5/14.
 */
var _ = require('koa-route');
var down = require('../lib/down.js');
var identify = require('../lib/identify.js');
var upload = require('../lib/upload.js');
var config = require('../config.js');

module.exports = function (app) {
  app.use(_.get('/identify', function * () {
    var response = this.response;
    var request = this.request;
    var ret = {};
    if (request.query.url) {
      var downOutput = yield down(request.query.url);
      var filename = downOutput.output;
      ret = yield identify(filename);
    } else {
      ret = {
        errno: -1,
        message: 'field is error'
      };
    }
    response.jsonp(ret);
  }));

  app.use(_.get('/upload', function * () {
    var response = this.response;
    var ret = {};
    if (this.query.url) {
      var downOutput = yield down(this.query.url);
      var filename = downOutput.output;
      ret = yield identify(filename);
      var uploadInfo = yield upload(filename, ret.type);
      ret.filename = uploadInfo.key;
      ret.domain = config.file.domain;
    } else {
      ret = {
        errno: -1,
        message: 'field is error'
      };
    }
    response.jsonp(ret);
  }));
};